package cn.luowb.checkchat.controller;

import cn.luowb.checkchat.common.result.Result;
import cn.luowb.checkchat.common.web.Results;
import cn.luowb.checkchat.dto.req.PostFeedDTO;
import cn.luowb.checkchat.dto.req.PostReqDTO;
import cn.luowb.checkchat.dto.resp.PostFeedRespDTO;
import cn.luowb.checkchat.dto.resp.PostRespDTO;
import cn.luowb.checkchat.service.PostService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Tag(name = "帖子")
public class PostController {

    @Autowired
    private PostService postService;

    /**
     * 发布帖子
     *
     * @param postReqDTO
     * @return
     */
    @PostMapping("/posts")
    public Result createPost(@RequestBody PostReqDTO postReqDTO) {
        //增加一个帖子
        Long postId = postService.createPost(postReqDTO);
        return Results.success(postId);
    }

    /**
     * 查看帖子内容
     */
    @GetMapping("/public/posts/{id}")
    public Result getPostDetailById(@PathVariable("id") Integer id) {
        PostRespDTO postRespDTO = postService.getPostDetailById(id);
        return Results.success(postRespDTO);
    }

    /**
     * 删除帖子内容
     */
    @DeleteMapping("/posts/{id}")
    public Result deletePost(@PathVariable("id") Integer id) {
        postService.deletePost(id);
        return Results.success();
    }

    /**
     * feed流
     */
    @GetMapping("/posts")
    public Result<PostFeedRespDTO> getFeedStreamPost(PostFeedDTO postFeedDTO) {
        postFeedDTO.setSize(postFeedDTO.getSize() == null ? 10L : postFeedDTO.getSize());
        PostFeedRespDTO postFeedRespDTO = postService.getFeedStreamPost(postFeedDTO);
        return Results.success(postFeedRespDTO);
    }

}
